package com.bytedance.frameworks.baselib.network.http.b.a;

import com.bytedance.frameworks.baselib.network.http.AppConfig;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.CookieJar;
import okhttp3.Dns;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;

public final class OkHttp3Builder {
    private OkHttpClient okHttpClient;
    private static int b = 0;

    public static void a(int arg1) {
        if (arg1 > 0 && b == 0) {
            b = arg1;
        }
    }

    private static void setSupportProtocols(OkHttpClient.Builder builder) {
        if (builder != null) {
            ArrayList protocols = new ArrayList();
            switch (b) {
                case 1: {
                    protocols.add(Protocol.HTTP_2);
                    break;
                }
                case 2: {
                    protocols.add(Protocol.SPDY_3);
                    break;
                }
                case 3: {
                    break;
                }
                default: {
                    protocols.add(Protocol.HTTP_2);
                    protocols.add(Protocol.SPDY_3);
                    break;
                }
            }

            b = 4;
            protocols.add(Protocol.HTTP_1_1);
            ArrayList unmodifiableProtocolsList = new ArrayList(Collections.unmodifiableList(protocols));
            if (!unmodifiableProtocolsList.contains(Protocol.HTTP_1_1)) {
                throw new IllegalArgumentException("protocols doesn\'t contain http/1.1: " + unmodifiableProtocolsList);
            } else if (unmodifiableProtocolsList.contains(Protocol.HTTP_1_0)) {
                throw new IllegalArgumentException("protocols must not contain http/1.0: " + unmodifiableProtocolsList);
            } else if (unmodifiableProtocolsList.contains(null)) {
                throw new IllegalArgumentException("protocols must not contain null");
            } else {
                if (unmodifiableProtocolsList.contains(Protocol.SPDY_3)) {
                    unmodifiableProtocolsList.remove(Protocol.SPDY_3);
                }
                builder.protocols(Collections.unmodifiableList(unmodifiableProtocolsList));
            }
        }
    }

    public final OkHttpClient build() {
        OkHttpClient.Builder builder;
        OkHttpClient okHttpClient;
        AppConfig.f();
        synchronized (AppConfig.class) {
            if (this.okHttpClient != null) {
                okHttpClient = this.okHttpClient;
                if (b > 0 && b < 4 && okHttpClient != null) {
                    builder = okHttpClient.newBuilder();
                    setSupportProtocols(builder);
                    builder.build();
                }
                okHttpClient = this.okHttpClient;
            } else {
                builder = new OkHttpClient.Builder();
                if (b > 0 && b < 4) {
                    setSupportProtocols(builder);
                }

                builder.connectionPool(new ConnectionPool(15, 180000, TimeUnit.MILLISECONDS));
                builder.connectTimeout(15000, TimeUnit.MILLISECONDS);
                builder.readTimeout(15000, TimeUnit.MILLISECONDS);
                builder.writeTimeout(15000, TimeUnit.MILLISECONDS);
                builder.addNetworkInterceptor(new b());
                builder.dns(new Dns() {
                    @Override
                    public final List<InetAddress> lookup(String hostname) throws UnknownHostException {
                        List<InetAddress> v0;
                        if (!AppConfig.g()) {
                            v0 = Dns.SYSTEM.lookup(hostname);
                            return v0;
                        }

                        v0 = null;
                        try {
                            AppConfig.b v1_1 = AppConfig.h();
                            if (v1_1 != null) {
                                v0 = v1_1.a(hostname);
                            }
                        } catch (Exception v1) {
                        }

                        if (v0 == null || (v0.isEmpty())) {
                            v0 = Dns.SYSTEM.lookup(hostname);
                        }

                        return v0;
                    }
                });
                builder.cookieJar(CookieJar.NO_COOKIES);
                builder.interceptors().add(new JavaNetCookieJar());
                this.okHttpClient = builder.build();
                return this.okHttpClient;
            }

            return okHttpClient;
        }
    }
}

